<template>
    <div class="financial">
        <div v-if="mainShow">
            <van-nav-bar :title="$t('financial.title')" left-arrow @click-left="_return" fixed :right-text="$t('financial.record')" @click-right="showRecord"></van-nav-bar>
            <div class="financial-main">
                <div class="financial-main-info">
                    <div class="financial-main-info-top">
                        <p class="financial-main-info-top-txt">
                            <span style="vertical-align: middle">{{ $t('financial.total') }}</span>
                            <img :src="eyeFlag ? openEye : closeEye" alt="" @click="eyeFlag = !eyeFlag" style="vertical-align: middle">
                        </p>
                        <p class="financial-main-info-top-price">
                            {{ priceCom(price) }}
                        </p>
                    </div>
                    <div class="financial-main-info-bottom">
                        <div class="financial-main-info-bottom-item">
                            <p class="financial-main-info-bottom-item__label">{{ $t('financial.sum') }}</p>
                            <p class="financial-main-info-bottom-item__value">{{total}}</p>
                        </div>
                        <div class="financial-main-info-bottom-item">
                            <p class="financial-main-info-bottom-item__label">{{ $t('financial.yesterday') }}</p>
                            <p class="financial-main-info-bottom-item__value">{{yesterday}}</p>
                        </div>
                    </div>
                </div>

                <ul class="financial-main-list">
                    <li class="financial-main-list-item" v-for="(item, key) in coinList" @click="showCurrent(item)" :key="key">
                        <div class="financial-main-list-label">
                            <div>
                                <img :src="'http://ht.aat.ink'+item.img" alt="">
                                <span style="vertical-align: middle">{{ item.unit }}</span>
                            </div>
                            <div >{{ $t('financial.capital_yesterday') }}</div>
                        </div>
                        <div class="financial-main-list-value">
                            <div><span class="price">{{ item.balance }}</span><span style="font-size: 14px;">{{ item.unit }}</span></div>
                            <div style="color:#f6543e;font-size: 14px;">
                                <div>{{item.current_profit}}{{$t('financial.ordtype1')}}</div>
                                 <div style="text-align: right;">{{item.regular_profit}}{{$t('financial.ordtype2')}}</div>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
            <div class="financial-footer">
                <button class="financial-footer-btn" @click="showCash">{{ $t('exchange_out.title') }}</button>
                <button class="financial-footer-btn" @click="showCharge">{{ $t('exchange_in.title') }}</button>
            </div>
        </div>
        <record v-if="recordShow" @_return="_returns"></record>
        <current v-if="currentShow" @_return="_returns" :coinName="coinName"></current>
        <cash v-if="cashShow" @_return="_returns"></cash>
        <charge v-if="chargeShow" @_return="_returns"></charge>
         <rec v-if="recShow" @_return="_returns"></rec>
    </div>
</template>

<script>
    import record from '@/components/coin/record'
    import current from '@/components/coin/current'
    import cash from '@/components/coin/cash'
    import charge from '@/components/coin/recharge'
      import rec from '@/components/coin/rec'
    export default {
        data() {
            return {
                openEye: require('../../assets/open-eye.png'),
                closeEye: require('../../assets/close-eye.png'),
                eyeFlag: true,
                page: 1,
                price: 0,
                coinList: [],
                mainShow:true,
                recordShow:false,
                currentShow:false,
                cashShow:false,
                recShow:false,
                chargeShow:false,
                listNum:0,
                coinName:'',
                yesterday: 0,
                total:0
            }
        },
        methods: {
            _return(){
                window.removeEventListener("scroll", this.reachBottom,true)
                this.$emit('_return')
            },
            initial() {
                this.coinList = []
                this.yesterday = this.price = this.total = 0
                this.$api.get('bi/bearInterestIndex',{page:this.page})
                    .then(res =>{
                        if(res.detail.record.length < 10){
                            window.removeEventListener("scroll", this.reachBottom,true)
                        }
                        res.detail.record.forEach(item => {
                            this.coinList.push(item)
                            this.price += Number(item.balance) * Number(item.bilv)
                            this.yesterday += Number(item.coinTotalProfit) * Number(item.bilv)
                            this.total += Number(item.profit) * Number(item.bilv)
                        })
                        this.listNum = res.detail.listNum
                    })
            },
            getScrollTop () {
                // 考虑到浏览器版本兼容性问题，解析方式可能会不一样
                return document.documentElement.scrollTop || document.body.scrollTop
            },
            getWinHeight () {
                // 浏览器可见内容高度 || 浏览器所有内容高度(考虑到浏览器版本兼容性问题，解析方式可能会不一样)
                return document.documentElement.clientHeight || document.body.clientHeight
            },
            getScrollHeight() {
                let bodyScrollHeight = 0
                let documentScrollHeight = 0
                if (document.body) {
                    bodyScrollHeight = document.body.scrollHeight
                }
                if (document.documentElement) {
                    documentScrollHeight = document.documentElement.scrollHeight
                }
                // 当页面内容超出浏览器可视窗口大小时，Html的高度包含body高度+margin+padding+border所以html高度可能会大于body高度
                return (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight
            },
            isReachBottom () {
                const scrollTop = this.getScrollTop() // 获取滚动条的高度
                const winHeight = this.getWinHeight() // 一屏的高度
                const scrollHeight = this.getScrollHeight() // 获取文档总高度
                return scrollTop >= parseInt(scrollHeight) - winHeight
            },
            reachBottom() {
                if (this.isReachBottom()) {
                    if (this.listNum > 10) {
                        this.page ++
                        this.initial()
                    } else {
                        window.onscroll = null
                    }
                }
            },
            showRecord(){
                this.mainShow = false
                this.recordShow = true
                window.removeEventListener("scroll", this.reachBottom,true)
            },
            _returns(){
                this.initial()
                this.mainShow = true
                this.recordShow = this.currentShow = this.cashShow = this.chargeShow = false 
                window.addEventListener("scroll", this.reachBottom,true)
            },
            showCurrent(data){
                window.removeEventListener("scroll", this.reachBottom,true)
                this.coinName = data.unit.toLowerCase()
                this.mainShow = false
                this.currentShow = true
            },
            showCash(){
                window.removeEventListener("scroll", this.reachBottom,true)
                this.mainShow = false
                this.cashShow = true
            },
            showCharge(){
                window.removeEventListener("scroll", this.reachBottom,true)
                this.mainShow = false
                this.chargeShow = true
            }
        },
        computed: {
            priceCom() {
                return function (price) {
                    return this.eyeFlag ? price : String(price).replace(/./g, '*')
                }
            },
        },
        created() {
            this.initial()
        },
        mounted() {
            this.$emit('disappear')
            window.addEventListener("scroll", this.reachBottom,true)
        },
        components:{
            record,
            current,
            cash,
            charge
        }
    }
</script>

<style scoped lang="stylus">
    [class*=van-hairline]::after{
        border: none;
    }
    .financial {
        /*background-color: #071E57;*/

        &-main {
            margin-top: 46px;
            padding: 15px;
            margin-bottom: 60px;

            &-info {
                background:linear-gradient(180deg, #54d8b4, #26c196)
                box-shadow:0px 6px 12px 0px rgba(0,0,0,0.5);
                border-radius 5px
                padding: 15px;

                &-top {
                    border-bottom: 1px solid rgba(255, 255, 255, 0.1);

                    &-txt {
                        font-size: 12px;
                        color: #fff;
                    }

                    &-price {
                        font-size: 36px;
                        line-height: 40px;
                        color: #fff;
                        margin-bottom: 8px;
                    }
                }

                &-bottom {
                    margin-top: 12px;
                    display: flex;
                    justify-content: space-between;

                    &-item {
                        &__label {
                            font-size: 12px;
                            color: #fff;
                            height: 16px;
                            line-height: 16px;
                        }

                        &__value {
                            margin-top: 4px;
                            font-size: 18px;
                            color: #115f49;
                            height: 22px;
                            line-height: 22px;
                        }

                        &:last-child {
                            text-align: right;
                        }
                    }
                }
            }

            &-list {
                &-item {
                    padding: 12px 15px;
                    background:none;
                    margin-top: 10px;
                    border-radius 5px
                    color #333
                    border-bottom 1px solid #ebebeb
                }

                &-label {
                    display: flex;
                    align-items: center;
                    justify-content: space-between;

                    div {
                        img {
                            width: 30px;
                            height: 30px;
                            vertical-align: middle;
                            margin-right: 3px;
                        }

                        font-size: 14px;

                        &:last-child {
                            opacity: 0.6;
                        }
                    }
                }

                &-value {
                    margin-top: 4px;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    font-size: 24px;

                    .price {
                        color: #23ba92;
                        margin-right: 2px;
                    }
                }
            }
        }

        &-cur-coin {
            width: 94%;
            margin: 0 auto;
            &-desc {
                padding: 15px;
                background-color: #0E2764;
                box-shadow: 0 2px 20px -6px rgba(0,0,0,0.42);
                &-top {
                    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
                    text-align: center;
                    &__label {
                        position: relative;
                        font-size: 12px;
                        line-height: 16px;
                        .coin-logo {
                            width: 30px;
                            height: 30px;
                            vertical-align: middle;
                            margin-right: 4px;
                        }
                        .eye-icon {
                            position: absolute;
                            top: 0;
                            right: 15px;
                        }
                    }
                    &__value {
                        font-size: 36px;
                        color: #44CEFA;
                        margin-bottom: 8px;
                    }
                }

                &-bottom {
                    margin-top: 13px;
                    &__label {
                        display: flex;
                        justify-content: space-between;
                        font-size: 12px;
                        line-height: 16px;
                    }
                    &__value {
                        display: flex;
                        justify-content: space-between;
                        font-size: 18px;
                        color: #44CEFA;
                        line-height: 22px;
                    }
                }
            }

            &-chart {
                margin-top: 40px;
                &-title {
                    text-align: center;
                    font-size: 36px;
                    color: #44CEFA;
                    line-height: 40px;
                }
                &-desc {
                    text-align: center;
                    font-size: 12px;
                    line-height: 16px;
                    opacity: 0.6;
                }
            }
        }
        &-turn-out, &-turn-in, &-record-list {
            padding: 60px 15px;
        }
        &-record-list {
            &-item {
                display: flex;
                justify-content: space-between;
                align-items: center;
                padding: 12px 10px;
                background-color: #0E2764;
                margin-bottom: 5px;
                &-desc {
                    display: flex;
                    align-items: center;
                    img {
                        width: 30px;
                        height: 30px;
                        margin-right: 5px;
                    }
                }
                &__label {
                    font-size: 14px;
                }
                &__time {
                    font-size: 12px;
                    opacity: 0.6;
                }
            }
        }

        &-turn-out, &-turn-in {
            &-info {
                background-color: #0E2764;
                padding: 12px 10px;
                display: flex;
                justify-content: space-between;

                &-wrap {
                    display: flex;
                    align-items: center;
                    img {
                        width: 30px;
                        height: 30px;
                        margin-right: 10px;
                    }
                }
                &-label {
                    font-size: 12px;
                    line-height: 16px;
                }
                &-value {
                    font-size: 14px;
                    color: #44CEFA;
                    line-height: 20px;
                }
            }
            &-inp {
                margin-top: 10px;
                background-color: #fff;
                padding: 10px;
                font-size: 14px;
                .van-cell.van-field {
                    margin-top: 20px;
                    background-color: #0E2764!important;
                    padding: 0;
                    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
                    .van-button {
                        border: none;
                        background-color: #0E2764;
                        color: #44CEFA;
                        &:active::before {
                            background-color: #0E2764;
                        }
                    }
                }
            }
            &-btn {
                margin-top: 20px;
                border: none;
                background-color: #4BC5F9;
                width: 100%;
                height: 44px;
                border-radius: 22px;
                color: #fff;
            }
        }

        &-turn-coin {
            &-header {
                height: 75px;
                line-height: 75px;
                display: flex;
                justify-content: space-between;
                align-items: center;

                .van-icon.van-icon-arrow-left {
                    font-size: 20px;
                }
            }
        }
        &-pay-pwd {
            height: 100%;
            background: #0E2764;

            &-tip {
                text-align: center;
                color: #FFD63B;
                font-size: 12px;
                margin-top: 10px;
            }

            &-btn {
                display: flex;
                justify-content: center;
                margin-top: 170px;
                padding-bottom: 15px;

                .van-button {
                    width: 96%;
                    border-radius: 22px;
                    color: #fff;
                    border: none;
                    background-color: #4BC5F9;
                }
            }
        }

        &-footer {
            position: fixed;
            left: 0;
            bottom: 0;
            width: 100%;
            box-sizing: border-box;
            background:#fff;
            box-shadow:0px -2px 20px -6px rgba(0,0,0,0.42);
            padding: 8px 10px;
            display: flex;
            justify-content: space-between;

            &-btn {
                border: none;
                padding: 0 70px;
                height: 44px;
                border-radius: 5px;
                color: #1F1F1F;
                background:linear-gradient(180deg,#54d8b4,#26c196);;
                color #fff
                &:first-child {
                    background:linear-gradient(180deg, #ff7541, #f6543e);
                }
            }
        }

        .van-popup {
            height: 50%;
        }
    }
</style>
